const db = require("../models");
const logger = require("../utils/_logger");
const DAO = require("../dao/DAO");
const Menus = db.models.menus;



/** 新增|修改 */
function addOrUpdate(data, cb) {
    if (!data.menu_type && data.menu_type != 0) return cb({ data: null, code: -1, message: "菜单类型为空！" });
    if (!data.title) return cb({ data: null, code: -1, message: "菜单名称为空！" });
    if (data.menu_type == 3 && !data.auths) return cb({ data: null, code: -1, message: "权限标识为空！" });
    let menu = {
        menu_type: data.menu_type,
        parent_id: data.parent_id,
        title: data.title,
        name: data.name,
        path: data.path,
        component: data.component,
        rank: data.rank ?? 99,
        redirect: data.redirect,
        icon: data.icon,
        extra_icon: data.extra_icon,
        enter_transition: data.enter_transition,
        leave_transition: data.leave_transition,
        active_path: data.active_path,
        auths: data.auths,
        frame_src: data.frame_src,
        frame_loading: data.frame_loading ?? true,
        show_link: data.show_link ?? true,
        show_parent: data.show_parent ?? false,
        keep_alive: data.keep_alive ?? false,
        hidden_tag: data.hidden_tag ?? false,
        fixed_tag: data.fixed_tag ?? false,
    }
    if (!data.id) { // 新增
        DAO.create(Menus, menu, res => {
            cb(res)
        })
    } else { // 修改
        DAO.update(Menus, menu, { id: data.id }, res => {
            cb(res)
        })
    }
}


/** 新增 */
exports.create = async (req, res) => {
    const pm = req.body;
    addOrUpdate(pm, (data) => {
        res.sendResult(data)
    })
};
/** 更新 */
exports.update = (req, res) => {
    const pm = req.body;
    // 请求验证
    if (!pm.id) return res.sendResult({ data: '', code: -1, message: "ID不能为空！" })
    addOrUpdate(pm, (data) => {
        res.sendResult(data)
    })
};
/** 所有list */
exports.findAll = (req, res) => {
    let pm = {
        current_page: 0,
        order: [['rank', 'asc']],
        params: {
            title: req.body?.title ? { [db.Op.substring]: req.body?.title ?? '' } : ''
        }
    }
    DAO.list(Menus, pm, list => {
        res.sendResult(list)
    })
};
/** 单个 */
exports.findOne = (req, res) => {
    const pm = req.body;
    DAO.findOne(Menus, pm, data => {
        res.sendResult(data)
    })
};


/** 删除 */
exports.delete = (req, res) => {
    const pm = req.body;
    // 请求验证
    if (!pm.id) return res.sendResult({ data: '', code: -1, message: "ID不能为空！" })
    Menus.destroy({ where: { [db.Op.or]: { id: pm.id, parent_id: pm.id } } }).then(_ => {
        res.sendResult({ data: '', code: 0, message: "删除成功！" })
    }).catch(err => {
        logger.error(err);
        res.sendResult({ data: '', code: -1, message: "删除失败！" })
    })
};

/** 删除全部 */
exports.deleteAll = (req, res) => {
    const pm = req.body;
    DAO.deleteAll(Menus, data => {
        res.sendResult(data)
    })
};

/** 获取简洁菜单list */
exports.list = (req, res) => {
    DAO.listAll(Menus, {
        order: [['rank', 'asc']],
        attributes: ["id", "parent_id", "menu_type", "title"]
    }, list => {
        res.sendResult(list)
    })
}


/** 原始查询 */
exports.query = (req, res) => {
    const pm = req.body;
    let sql = 'SELECT * FROM `users`'
    DAO.doQuery(sql, data => {
        res.sendResult(data)
    })
};
